﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using GodPl;
using GodPl.WorldState;
using GodPl.GameObjects;

namespace GodPl.GameObjects
{
    public class AppleTree : GameObject, IObstacle, IFoodSource, IHittable
    {
        Texture2D[] sprite = new Texture2D[4];

        static readonly int MaxApples = 10;
        int numApples = 1;

        int NumApples
        {
            get { return numApples; }
            set
            {
                if (value < 0)
                {
                    numApples = 0;
                }
                else if (value > MaxApples)
                {
                    numApples = MaxApples;
                }
                else
                {
                    numApples = value;
                }
            }
        }

        public AppleTree(World world, Vector2 position)
            : base(world, position)
        {

            LoadContent();
            spriteScale = 0.7f;

            localBounds.Width = (int)Math.Round(sprite[0].Width * spriteScale);
            localBounds.Height = (int)Math.Round(sprite[0].Height * spriteScale);
        }


        public override void LoadContent()
        {
            sprite[0] = World.Content.Load<Texture2D>("Sprites/Tree-0A-200");
            sprite[1] = World.Content.Load<Texture2D>("Sprites/Tree-1A-200");
            sprite[2] = World.Content.Load<Texture2D>("Sprites/Tree-2A-200");
            sprite[3] = World.Content.Load<Texture2D>("Sprites/Tree-3A-200");
        }

        public override void Update(GameTime gameTime)
        {
            if (GodPlGame.Every(0, 0, 8) && GodPlGame.Roll(0.5f))
            {
                NumApples++;
            }
            if (GodPlGame.Every(0, 0, 5) && GodPlGame.Roll(0.6f))
            {
                NumApples++;
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Texture2D sprite = this.sprite[Math.Min(this.numApples,3)];
            spriteBatch.Draw(sprite, Position, null, Color.White, 0f, new Vector2(sprite.Width / 2, sprite.Height / 2), spriteScale, SpriteEffects.None, 0f);
        }


        #region IFoodSource Members

        public void BeEatenFrom(GodPl.GameObjects.Creatures.Briyya actor)
        {
            NumApples--;
        }

        public float Nutritiousness
        {
            get { return 0.5f; }
        }

        public float Toxicity
        {
            get { return 0f; }
        }

        #endregion

        #region IObstacle Members

        public ObstacleGravityType GravityType
        {
            get { return ObstacleGravityType.Point; }
        }

        #endregion

        #region IFoodSource Members


        public bool IsStocked
        {
            get { return (numApples > 0); }
        }

        #endregion

        #region IHittable Members

        public void BeHit(GodPl.GameObjects.Creatures.Briyya actor, float intensity)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
